<template>
	<!-- 最外层的容器 -->
	<view class="my-settle-container">
		<!-- 全选区域 -->
		<label class="radio">
			<radio color="#C00000" :checked="isFullChecked" @click="onChange" /><text>全选</text>
		</label>

		<!-- 合计区域 -->
		<view class="amount-box">
			合计:<text class="amount">￥{{setPrice}}</text>
		</view>

		<!-- 结算按钮 -->
		<view class="btn-settle" @click="onSettle">结算({{checkedCount}})</view>
	</view>
</template>

<script>
	import toast from '@/utils/toast.js'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import {
		createOrder,
		getOrder
	} from '../../api/user';
	export default {
		name: "mt-settle",
		data() {
			return {

			};
		},
		computed: {
			...mapGetters('my_cart', ['checkedCount', 'total', 'setPrice']),
			isFullChecked() {
				return this.checkedCount === this.total
			}
		},
		methods: {
			...mapMutations('my_cart', ['updateAllCart']),
			onChange() {
				this.updateAllCart(!this.isFullChecked)
			},
			onSettle() {
				// 2. 再判断用户是否选择了收货地址
				if (!this.$store.state.my_user.address.cityName) return toast({
					title: '请选择收货地址！'
				})
				// 1. 先判断是否勾选了要结算的商品
				if (!this.checkedCount) return toast({
					title: '请选择要结算的商品！'
				})
				// 3. 最后判断用户是否登录了
				if (!this.$store.state.my_user.token) return uni.showModal({
					title: '盒马优购',
					content: '是否跳转到登录页面重新登录？',
					success({
						cancel
					}) {
						if (cancel) return
						uni.switchTab({
							url: '/pages/my/my'
						})
					}
				})
				this.payCart()
			},
			async payCart() {
				const selectGoods = this.$store.state.my_cart.cart.filter(item => item.goods_state)
				const goods = selectGoods.map(item => {
					return {
						goods_id: item.goods_id,
						goods_number: item.goods_count,
						goods_price: item.goods_price
					}
				})
				const res = createOrder({
					order_price: this.setPrice,
					consignee_addr: this.$store.getters['my_user/addressStr'],
					goods
				})
				console.log(res.message.order_number)
				const payRes = await getOrder({
					order_number: res.message.order_number
				})
				console.log(payRes.message.pay)
				const pay = uni.requestPayment(payRes.message.pay)
				console.log(pay)
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 50px;
		// 将背景色从 cyan 改为 white
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-left: 5px;
		font-size: 14px;
		z-index: 999;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount {
			color: #c00000;
		}

		.btn-settle {
			height: 50px;
			min-width: 100px;
			background-color: #c00000;
			color: white;
			line-height: 50px;
			text-align: center;
			padding: 0 10px;
		}
	}
</style>
